package collector;

import java.io.IOException;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import extractor.StatusData;


public abstract class StatusListener implements MessageListener {

	/**
	 * An Abstract class which defines a skeleton onMessage method which performs validation of the message and then its processing.
	 * The abstract process method defines what will be done with the received data.
	 */
	@Override
	public final void onMessage(Message message) {
		//System.out.println("onMessage");
		try {
			// Validate the types of the message
			if (message instanceof ObjectMessage && ((ObjectMessage) message).getObject() instanceof StatusData) {
				StatusData data = (StatusData) ((ObjectMessage) message).getObject();
				// Validate data before processing
				if (validate(data)) {
					process(data);
				} 
				else {
				}
			}			
		} catch (JMSException | IOException e) {
		} 
	}
	
	/**
	 * The method is to validate the correctness of the received data
	 */
	public abstract boolean validate(StatusData data);
	
	/**
	 * The method is to process the received data
	 */
	public abstract void process(StatusData data) throws IOException;


}
